package bitSetDemo0;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-05-03
 * Time: 10:43
 */
public class MyBitSet {
    private byte[] elem;
    private int usedSized;
    public MyBitSet(){
        this.elem = new byte[1];
    }
    public MyBitSet(int n){
        this.elem = new byte[n/8+1];
    }
    public void set(int val){
        if(val < 0){
            throw new ArrayIndexOutOfBoundsException();
        }
        int elemIndex = val/8;
        int biteIndex = val%8;
        if(elem[elemIndex]== (elem[elemIndex] |= (1<<biteIndex))){
            return;
        }
        elem[elemIndex] |= (1<<biteIndex);

        usedSized++;
    }
    public void reSet(int val){
        if(val<0){
            throw new ArrayIndexOutOfBoundsException();
        }
        int elemIndex = val/8;
        int biteIndex = val%8;
        elem[elemIndex] &=~(1<<biteIndex);
        usedSized--;
    }
    public boolean find(int val){
        if(val < 0){
            return false;
        }
        int elemIndex = val/8;
        int biteIndex =val%8;
       if((elem[elemIndex]&=1<<biteIndex) != 0){
           return true;
       }
       return false;
    }
    public int getUsedSized(){
        return usedSized;
    }

}
